package com.ylr.config.framework.controller.web;

import java.util.List;
import java.util.Map;

import com.ylr.config.common.dto.mapping.MappingDto;
import com.ylr.config.common.vo.mapping.MappingVo;
import com.ylr.config.framework.service.MappingService;
import com.ylr.base.common.annotation.Resubmit;
import com.ylr.base.common.jsr303.group.Save;
import com.ylr.base.common.jsr303.group.Update;
import com.ylr.base.common.tool.Result;
import com.ylr.config.common.dto.mapping.SimpleMappingDto;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Size;


/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-05-02 18:58:56
 * className: MappingWebController
 * version: 1.0
 * description:
 */
@Validated
@RestController
@RequestMapping("/web/mapping")
@AllArgsConstructor
public class MappingWebController {

    private static final Logger log = LoggerFactory.getLogger(MappingWebController.class);

    private final MappingService mappingService;

    /**
     * 保存键值对映射接口
     * <p>
     * 此接口会校验是否重复提交
     * @param mappingVo 参数对象
     */
    @Resubmit(message = "保存键值对映射")
    @PostMapping("/save")
    public Result<Object> saveMapping(@Validated(Save.class) @RequestBody MappingVo mappingVo) {
        log.info("web => 开始保存键值对映射操作。{}", mappingVo);

        mappingService.saveMapping(mappingVo);

        log.info("web => 保存键值对映射操作结束");
        return Result.success();
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result<Object> list(@RequestParam Map<String, Object> params) {
//        PageUtils page = mappingService.queryPage(params);

        return Result.success();
    }

    /**
     * 获取键值对映射接口
     * @param id 主键
     * @return 键值对映射数据
     */
    @GetMapping("/get")
    public Result<MappingDto> getMapping(@RequestParam("id") Long id) {
        log.info("web => 开始获取键值对映射操作。id={}", id);

        MappingDto mappingDto = mappingService.getMapping(id);

        log.info("获取键值对映射操作结束");
        return Result.success(mappingDto);
    }

    /**
     * 获取键值对映射接口
     * @param key 键
     * @return 键值对映射数据
     */
    @GetMapping("/find")
    public Result<SimpleMappingDto> getMapping(@RequestParam("key") String key) {
        log.info("web => 开始获取键值对映射操作。key={}", key);

        SimpleMappingDto simpleMappingDto = mappingService.getSimpleMapping(key);

        log.info("web => 获取键值对映射操作结束");
        return Result.success(simpleMappingDto);
    }


    /**
     * 修改键值对映射接口
     * <p>
     * 此接口会校验是否重复提交
     * @param mappingVo 参数对象
     */
    @Resubmit(message = "修改键值对映射")
    @PostMapping("/update")
    public Result<Object> updateMapping(@Validated(Update.class) @RequestBody MappingVo mappingVo){
        log.info("web => 开始修改键值对映射操作。{}", mappingVo);

        mappingService.updateMapping(mappingVo);

        log.info("web => 修改键值对映射操作结束");
        return Result.success();
    }

    /**
     * 删除键值对映射接口
     * @param idList 主键列表
     */
    @DeleteMapping("/delete")
    public Result<Object> deleteMapping(@Size(min = 1, message = "主键列表不能为空") @RequestBody List<Long> idList){
        log.info("web => 开始删除键值对映射操作。idList={}", idList);

        mappingService.deleteMapping(idList);

//		mappingService.removeByIds(Arrays.asList(ids));

        log.info("web => 删除键值对映射操作结束");
        return Result.success();
    }

}
